package com.collect.tanzania.zhiScene.zhiWidget.zhiStepView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiScene.zhiWidget.zhiMenuView.ZHIOnMenuItemDidChangedListener;

public class ZHINextStepView extends LinearLayout implements View.OnClickListener {

    private final Context mContext;
    private View mView;
    private String mainText = getResources().getString(R.string.btn_next);
    private int mainTextColor = getResources().getColor(R.color.white);
    private Drawable backgroundDrawable = getResources().getDrawable(R.drawable.bg_solid_theme_color_0_corners_4dp);

    private TextView btn_next;

    private ZHIOnMenuItemDidChangedListener onMenuItemDidChangedListener;

    public ZHINextStepView(Context context) {
        this(context, null);
    }

    public ZHINextStepView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHINextStepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ZHINextStepView);
        mainText = typedArray.getString(R.styleable.ZHINextStepView_mainText);
        mainTextColor = typedArray.getColor(R.styleable.ZHINextStepView_mainTextColor, getResources().getColor(R.color.white));
        backgroundDrawable = typedArray.getDrawable(R.styleable.ZHINextStepView_backgroundDrawable);
        typedArray.recycle();

        initView();
    }

    @Override
    public void onClick(View v) {
        if (this.onMenuItemDidChangedListener != null) {
            this.onMenuItemDidChangedListener.OnMenuItemDidChanged("");
        }
    }

    /**
     * 初始化组件数据
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-10-25 19:43
     */
    private void initView() {
        mView = View.inflate(mContext, R.layout.view_next_step, this);
        btn_next = findViewById(R.id.btn_next);

        if(backgroundDrawable != null){
            btn_next.setBackground(backgroundDrawable);
        }

        btn_next.setText(mainText);
        btn_next.setTextColor(mainTextColor);

        btn_next.setOnClickListener(this);
        mView.setOnClickListener(this);
    }

    /**
     * 设置显示内容
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-10-25 19:58
     */
    public void setText(String text) {
        mainText = text;
        btn_next.setText(mainText);
    }

    /**
     * 获取显示内容
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-10-25 19:58
     */
    public String getText() {
        return btn_next.getText().toString().trim();
    }

    /**
     * 设置显示背景
     * @type  method  
     * @author liwenjie
     * @creatTime 2019-12-31 15:33
     */
    public void setButtonBackground(Drawable resId){
        backgroundDrawable = resId;

        if(backgroundDrawable != null){
            btn_next.setBackground(backgroundDrawable);
        }
    }

    /**
     * 设置监听
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-11-04 10:15
     */
    public void setOnMenuItemDidChangedListener(ZHIOnMenuItemDidChangedListener onMenuItemDidChangedListener) {
        this.onMenuItemDidChangedListener = onMenuItemDidChangedListener;
    }
}
